package com.diandian.backend.dingding.data.service;

import com.diandian.backend.dingding.data.dto.DingdingDeptTreeListParamDTO;
import com.diandian.backend.dingding.data.dto.DingdingDeptTreeStaffListParamDTO;
import com.diandian.backend.dingding.data.dto.DingdingStaffDetailParamDTO;
import com.diandian.backend.dingding.data.resp.DingdingDeptTreeListResp;
import com.diandian.backend.dingding.data.resp.DingdingDeptTreeStaffListResp;
import com.diandian.backend.dingding.data.resp.DingdingStaffDetailResp;
import com.diandian.base.boot.starter.exception.DefaultException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DingdingDeptTreeStaffOpenApiService {

    @Autowired
    private DingdingDeptTreeStaffService dingdingDeptTreeStaffService;

    @Autowired
    private DingdingStaffService dingdingStaffService;

    @Autowired
    private DingdingDeptTreeService dingdingDeptTreeService;

    public List<DingdingDeptTreeListResp> getDeptByJobNumber(String belong, String jobNumber, Boolean deepFlag) {

        List<DingdingDeptTreeListResp> resultList = new ArrayList<>();

        DingdingStaffDetailParamDTO staffDetailParamDTO = new DingdingStaffDetailParamDTO();
        staffDetailParamDTO.setBelong(belong);
        staffDetailParamDTO.setJobNumber(jobNumber);
        DingdingStaffDetailResp staffDetailResp = dingdingStaffService.getDetail(staffDetailParamDTO);
        if(staffDetailResp == null) {
            throw new DefaultException("不存在此工号");
        }
        String userId = staffDetailResp.getUserId();

        DingdingDeptTreeStaffListParamDTO deptTreeStaffListParamDTO = new DingdingDeptTreeStaffListParamDTO();
        deptTreeStaffListParamDTO.setBelongEql(belong);
        deptTreeStaffListParamDTO.setUserIdEql(userId);
        List<DingdingDeptTreeStaffListResp> deptTreeStaffListResps =  dingdingDeptTreeStaffService.getList(deptTreeStaffListParamDTO);
        if(deptTreeStaffListResps == null || deptTreeStaffListResps.size() == 0) {
            throw new DefaultException("未关联任何部门");
        }
        List<String> deptCodeList = deptTreeStaffListResps.stream().map(DingdingDeptTreeStaffListResp::getDeptCode).collect(Collectors.toList());

        // 查询本级
        DingdingDeptTreeListParamDTO deptTreeListParamDTO = new DingdingDeptTreeListParamDTO();
        deptTreeListParamDTO.setBelongEql(belong);
        deptTreeListParamDTO.setDeptCodeIn(deptCodeList);
        resultList.addAll(dingdingDeptTreeService.getList(deptTreeListParamDTO));
        if(deepFlag == null || !deepFlag) {
            return resultList;
        }

        // 增加下级
        List<DingdingDeptTreeListResp> deepList = new ArrayList<>();
        for (int i = 0; i < resultList.size(); i++) {
            String deptCode = resultList.get(i).getDeptCode();
            List<DingdingDeptTreeListResp> itemList = dingdingDeptTreeService.getDeepListByCode(belong, deptCode);
            if(itemList == null || itemList.size() == 0) {
                continue;
            }
            deepList.addAll(itemList);
        }
        return deepList;
    }
}
